package com.retrifot2.myretrifot2.Acitivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.retrifot2.myretrifot2.R;
import com.retrifot2.myretrifot2.ServerUnitls;
import com.retrifot2.myretrifot2.modle.HongyanquanResponse;
import com.retrifot2.myretrifot2.requestServiice.RequestserviceGetData;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private TextView t1;
    private RequestserviceGetData requestservice;
    private Retrofit retrofit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t1 = findViewById(R.id.t1);
        t1.setOnClickListener(this);
//获取Retrofit对象，设置地址


        ServerUnitls serverUnitls = ServerUnitls.getInstance();

        requestservice= serverUnitls.getRequestservice();
    }

    private void getRequestServer() {
//        Call<HongyanquanResponse> hyqData = requestservice.getHyqData("100577");
        Call<HongyanquanResponse> hyqData = requestservice.getHyqDataPost("100577");

        hyqData.enqueue(new Callback<HongyanquanResponse>() {
            @Override
            public void onResponse(Call<HongyanquanResponse> call, Response<HongyanquanResponse> response) {
                successInfo(response);
            }

            @Override
            public void onFailure(Call<HongyanquanResponse> call, Throwable t) {
                t1.setText(t.toString());
            }
        });
    }
   public void  requestServerRx(){
       Observable<HongyanquanResponse> hyqDataPostRx = requestservice.getHyqDataPostRx("100577");
       hyqDataPostRx.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Observer<HongyanquanResponse>() {
                   @Override
                   public void onSubscribe(Disposable d) {

                   }

                   @Override
                   public void onNext(HongyanquanResponse hongyanquanResponse) {
                       String s = hongyanquanResponse.getData().toString();
                       Log.e("LOG_",s);
                       t1.setText(s);
                   }

                   @Override
                   public void onError(Throwable e) {

                   }

                   @Override
                   public void onComplete() {

                   }
               });



    }

    public void successInfo(Response<? extends ResponseBody> t)  {
        if (t.isSuccessful()) {
            Log.i("LHD", t.body().toString());
            //返回的结果保存在response.body()中
            ResponseBody body = t.body();
            String result = null;

                result = body.toString();

            //onResponse方法是运行在主线程也就是UI线程的，所以我们可以在这里
            //直接更新UI
            t1.setText(result);
        }
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.t1:
//                getRequestServer();
                requestServerRx();
                break;
        }
    }
}
